package net.osweb.core.tools.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

public class UtilTools {
	
	//判断空
	public static boolean isEmpty(String str){
		
		if(null == str || str.trim().length() <= 0)
			return true;
		return false;
	}
	
	public static boolean isNotEmpty(String str){
		if(null == str || str.trim().length() <= 0)
			return false;
		return true;
	}
	
	public static boolean isNotEmpty(Object objStr){
		if(null == objStr || objStr.toString().trim().length() <= 0)
			return false;
		return true;
	}
	
	public static boolean isEmpty(Double value){
		if(value == null){
			return true;
		}
		return false;
	}
	
	public static boolean isEmpty(Long value){ 
		if(value == null){
			return true;
		}
		return false;
	}	
	
	
	//判断date空
	public static boolean isEmpty(Date d){
		
		if(d == null){
			return true;
		}
		return false;
	}
	
	//判断是否为数据
	public static boolean isNumeric(String str) {
		if(UtilTools.isEmpty(str))
			return false;
		for (int i = str.length(); --i >= 0;) {
			if (!Character.isDigit(str.charAt(i))) {
				return false;
			}
		}
		return true;
	}

	
	//  GENERAL_PUNCTUATION 判断中文的“号  
	  
    //  CJK_SYMBOLS_AND_PUNCTUATION 判断中文的。号  
  
    //  HALFWIDTH_AND_FULLWIDTH_FORMS 判断中文的，号  
  
    public static boolean isChinese(char c) {  
  
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);  
  
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS  
  
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS  
  
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A  
  
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION  
  
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION  
  
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {  
  
            return true;  
  
        }  
  
        return false;  
  
    }  
  
  
    //判断是否为中文
    public static void isChinese(String strName) {  
  
        char[] ch = strName.toCharArray();  
  
        for (int i = 0; i < ch.length; i++) {  
  
            char c = ch[i];  
  
            if (isChinese(c) == true) {  
   
  
                return;  
  
            } else {  
  
  
                return;  
  
            }  
  
        }  
  
    }  
    
    public static Long parseLong(String value){
    	if(value == null){
    		return null;
    	}
    	try{
    		return Long.parseLong(value);
    	}catch(Exception e){
    		return null;
    	}
    }
    
    public static Integer parseInteger(String value){
    	if(value == null){
    		return null;
    	}
    	try{
    		return Integer.parseInt(value);
    	}catch(Exception e){
    		return null;
    	}
    }   
    
    public static Double parseDouble(String value){
    	if(value == null){
    		return null;
    	}
    	try{
    		return Double.parseDouble(value);
    	}catch(Exception e){
    		return null;
    	}
    }       
    
    public static Date parseDate(String value,String pattern){
    	if(isEmpty(pattern)){
    		pattern = "yyyy-MM-dd";
    	}
    	
    	if(value == null){
    		return null;
    	}
    	
    	try{
    		SimpleDateFormat format = new SimpleDateFormat(pattern);
    		return format.parse(value); 
    	}catch(Exception e){
    		return null;
    	}
    }    
    
    public static String formatDate(Date value,String pattern){
    	if(isEmpty(pattern)){
    		pattern = "yyyy-MM-dd";
    	}
    	
    	if(value == null){
    		return null;
    	}
    	
    	try{
    		SimpleDateFormat format = new SimpleDateFormat(pattern);
    		return format.format(value); 
    	}catch(Exception e){
    		return null;
    	}
    }        
    
	public static boolean isNumber(String arg){
		final String numbers = "1234567890.";
		if(arg == null || "".equals(arg)){
			return false;
		}
		char[] cs = arg.toCharArray();
		for(char c : cs ){
			int i = (int)c;
			if(numbers.indexOf(i) == -1){
				return false;
			}
		}
		return true;
	}
	
	public static boolean isInt(String arg){
		final String numbers = "1234567890";
		if(arg == null || "".equals(arg)){
			return false;
		}
		char[] cs = arg.toCharArray();
		for(char c : cs ){
			int i = (int)c;
			if(numbers.indexOf(i) == -1){
				return false;
			}
		}
		return true;
	}	     

}
